package com.bicon.botu.mall.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.base.entity.LogisticsCenter;
import com.bicon.botu.mall.base.mapper.LogisticsCenterMapper;
import com.bicon.botu.mall.base.service.ILogisticsCenterService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @auther 裴志鹏
 * @date 2017/9/8 11:18
 */
@Service
@Transactional
@Slf4j
public class LogisticsCenterServiceImpl extends ServiceImpl<LogisticsCenterMapper, LogisticsCenter> implements ILogisticsCenterService {
    /**
     * 新增物流信息
     *
     * @param logisticsCenterJson 物流信息Json
     * @return
     */
    @Override
    public BaseResult add(String logisticsCenterJson) {
        List<LogisticsCenter> logisticsCenters = JSONObject.parseArray(logisticsCenterJson, LogisticsCenter.class);
        if (logisticsCenters == null || logisticsCenters.size() < 0) {
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        for (LogisticsCenter l : logisticsCenters) {
            LogisticsCenter logisticsCenter = new LogisticsCenter();
            BeanMapper.copy(l, logisticsCenter);
            logisticsCenter.preInsert();
            boolean flag = logisticsCenter.insert();
            if (!flag) {
                return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
            }
        }
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改物流信息
     *
     * @param logisticsCenterJson 物流信息Json
     * @return
     */
    @Override
    public BaseResult update(String logisticsCenterJson) {
        List<LogisticsCenter> logisticsCenters = JSONObject.parseArray(logisticsCenterJson, LogisticsCenter.class);
        if (logisticsCenters == null || logisticsCenters.size() < 0) {
            return new BaseResult(Constants.RESPONSE_CODE_401, Constants.RESPONSE_MESSAGE_401);
        }
        for (LogisticsCenter l : logisticsCenters) {
            LogisticsCenter logisticsCenter = new LogisticsCenter();
            BeanMapper.copy(l, logisticsCenter);
            logisticsCenter.preUpdate();
            boolean flag = logisticsCenter.updateById();
            if (!flag) {
                return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
            }
        }
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }
}
